#{extends 'main.html' /}
#{set title:'CoolSoft - Demo Entries' /}
#{set roles4:''/}
*{userStories ---> List of all user stories in this project}*
<h2>Create Demo Entry</h2><hr/>
<div id="done">
Demo Entry added successfully !	
</div>
<div id='demo_entry'>
	
	<form action="javascript:send_to_controller()" name="demoEntry" class="jqtransform" id="form" method="POST">
		
		Description*:
		<br>
		<br>
		<textarea rows="10" cols="30" name="story" id="demo" class="validate[required]"></textarea>		
		<br>		
		Type*:		
		<br>		
		<select name="type" id="type">
			<option value="Sucess">Sucess</option>
			<option value="Failure">Failure</option>
		</select>
		<br>
		<br>
		<br>		
		<a id="add_story"href="#">+ Associate with a user story*</a>
		<br>
		<br>
		<input type="submit" name="submit" class="button" id="submit_btn" value="Submit" />	
		<div id='user_story'>
			<br>
			#{list items:userStories, as:'stories'}
				#{list items:stories.appRoles, as:'roles'}
					#{set roles4:roles4+roles.name+", " /}
				#{/list}
			<br>
			<div id="listStories">
				&nbsp;<input type="radio" name="userStoriesId" value="${stories.getEntityId()}" id="user_story" />As a/an #{get 'roles4' /} ${stories.story}
				#{set roles4:''/}
				<br>
				<br>
				#{/list}
				
			</div>
			<br>
			<br>
		</div>		
			
		</form>
		
</div>
<script type="text/javascript">
	$(document).ready(function(){
		$("#user_story").hide();
		$("#done").hide();
		$("#add_story").colorbox({inline:true, href:"#user_story",
		onOpen:function(){ $("#user_story").show(); },
		onCleanup:function(){ $("#user_story").hide(); },
		});				
	});
	function send_to_controller(){
		if (undefined === $("input[name='userStoriesId']:checked").val()) {
    		$.validationEngine.buildPrompt("#add_story","* Association is required","error");
			return false;			
		}
		else {
			 $.validationEngine.closePrompt("#add_story");
		}
		var desc = $('#demo').val();
		var type = $('#type').val();
		var storiesId =[];
		$('#listStories :checked').each(function() {
			storiesId.push($(this).val());	
		});
		if ( storiesId[0] == null ){			
			storiesId.push(0);
		}
		$.post(
		'@{DemoEntries.associateDemoEntry()}',
		{demo: desc, type: type, storiesId: storiesId}, // firebug
		function( temp1 ){
			//Means error, duplicate
			if ( temp1.length != 0 ){
				$.fn.colorbox.close();				
				showDialog('Error', 'Duplicate Demo Entry', 'error'); 
			}
			else{
				$("#demo_entry").fadeOut('slow', function () {
				//$('#done').fadeIn('slow');
				showDialogHelper('Done','Demo Entry added successfully !',false,'','success');
				$('#demo').val("");
				$("#demo_entry").fadeIn('slow');	
				});	
			}
		
			
			
		});
			
		
	}

</script>